new Vue({
    el: '#app',
    data: {
        formValidate: {
            phone: '',
            password: '',
            captcha: '',
            verify: ''
        },
        ruleValidate: {
            phone: [
                { required: true, message: '手机号一定要填哦', trigger: 'blur' },
                { type: 'string', pattern: /^1[3456789][0-9]{9}$/, message: '手机号格式错误', trigger: 'blur' },
            ],
            password: [
                { required: true, message: '密码一定要填哦', trigger: 'blur' },
            ],
            captcha: [
                { required: true, message: '验证码一定要填哦', trigger: 'blur' }
            ],
            verify: [
                { required: true, message: '短信验证码一定要填哦', trigger: 'blur' }
            ],
        },
        verifyTxt:'获取短信验证码',
        count: '',
        timer: null,
        verify_disabled: true,
        cancelTime: [],
        verify_loading:false,
        send_loading: false,
        errors:{
            phone:'',
            password:'',
            captcha:'',
            verify:''
        },
    },
    methods: {
        handlePhone(){
            // 验证手机号
            if((/^1[3456789][0-9]{9}$/.test(this.formValidate.phone))){
                this.verify_disabled = false
            }else{
                this.verify_disabled = true
            }
        },
        checkCaptcha(){
            var that = this
            axios.post('/checkCaptcha',{captcha:that.formValidate.captcha}).then(function (response) {
                if (response.data.code) {
                    that.captchaStatus = false
                    // that.errors.captcha = '验证码错误';
                    that.$Message.error('验证码错误');
                }else{
                    that.captchaStatus = true
                }
            }).catch(function (error) {
                that.captchaStatus = false
                // that.errors.captcha = '验证码错误';
                that.$Message.error('验证码错误');
            });
        },
        changeCaptcha(){
            this.captchaStatus = false
            let res = this.$refs.captcha.src
            this.$refs.captcha.src = res+'?'+Math.random()
        },
        sendVerify(){
            //验证码验证
            if(!this.captchaStatus){
                this.$Message.error('验证码错误');
                return
            }
            var that  = this
            if (!that.captchaStatus) {
                that.errors.captcha = '验证码错误'
                return
            }
            // 发送验证码
            that.verify_loading = true
            const TIME_COUNT = 60;
            var formData = {
                'mobile': that.formValidate.phone
            }
            axios.post('/sms/verify-code',formData).then(function (response) {
                that.verify_loading = false
                if(response.data.success){
                    that.verify_disabled = true
                    if (!that.timer) {
                        that.count = TIME_COUNT;
                        that.timer = setInterval(() => {
                            if (that.count > 0 && that.count <= TIME_COUNT) {
                                that.count--;
                                that.verifyTxt = that.count+'秒后再次发送'
                            } else {
                                that.verifyTxt = '获取短信验证码'
                                clearInterval(that.timer);
                                that.timer = null;
                                that.verify_disabled = false
                            }
                        }, 1000)
                    }
                }else{
                    that.$Message.error(response.data.message)
                }
            }).catch(function (error) {
                that.verify_loading = false
            });
        },
        handleSubmit(){
            // 登录
            this.send_loading = true
            var formData = this.formValidate
            var that  = this
            axios.post('/member/login',formData).then(function (response) {
                that.send_loading = false
                if(!response.data.code){
                    that.formValidate = {
                        phone: '',
                        password: '',
                        verify: '',
                        captcha: ''
                    }
                    that.$Message.success(response.data.msg)
                }else{
                    that.$Message.error(response.data.msg)
                    that.changeCaptcha()
                }
            }).catch(function (error) {
                that.send_loading = false
                that.changeCaptcha()
                if (typeof error.response.data.errors.phone != "undefined") {
                    that.errors.phone = error.response.data.errors.phone[0]
                }
                if (typeof error.response.data.errors.password != "undefined") {
                    that.errors.password = error.response.data.errors.password[0]
                }
                if (typeof error.response.data.errors.captcha != "undefined") {
                    that.errors.captcha = error.response.data.errors.captcha[0]
                }
                if (typeof error.response.data.errors.verify != "undefined") {
                    that.errors.verify = error.response.data.errors.verify[0]
                }
            });
        },
    },
})